package cn.bdqn.controller;

import cn.bdqn.pojo.Role;
import cn.bdqn.pojo.SysRight;
import cn.bdqn.service.RoleService;
import cn.bdqn.service.SysRightService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/role")
public class RoleController {
    @Resource
    RoleService roleService;

    @Resource
    SysRightService sysRightService;


    @RequestMapping("/findAllRoles")
    @ResponseBody
    public Object findAllRoles(){
        List<Role> list = roleService.findAllRoles();
        return list;
    }

    @RequestMapping("/list")
    public String list(String roleName, @RequestParam(defaultValue = "1") Integer pageIndex, Model model ){
        Integer pageSize=3;

        IPage<Role> roleIPage = roleService.selectList(pageIndex, pageSize, roleName);
        model.addAttribute("rolePage",roleIPage);
        model.addAttribute("roleName",roleName);
        return "role/list";
    }
    @RequestMapping("/add")
    public String add(Model model){
        List<SysRight> sysRights = sysRightService.selectAll();
        model.addAttribute("rights",sysRights);
        return "role/add";
    }
    @RequestMapping("/save")
    public String save(Role role,String[] rightCodes){
        try {
            roleService.insert(role,rightCodes);
            return "redirect:/role/list";
        } catch (Exception e) {
            e.printStackTrace();
            return "redirect:/role/add";
        }

    }

    @RequestMapping("/edit")
    public String edit(Long roleId,Model model){
        Role role = roleService.selectOne(roleId);
        model.addAttribute("role",role);
        //所有权限
        List<SysRight> sysRights = sysRightService.selectAll();
        model.addAttribute("rights",sysRights);
        //用户的权限
        List<SysRight> sysRights1 = sysRightService.selectRoleId(roleId);
        model.addAttribute("roleRights",sysRights1);
        return "role/edit";
    }

    /**
     * 修改
     * @param role
     * @param rightCodes
     * @return
     */
    @RequestMapping("/update")
    public String update(Role role,String[] rightCodes){

        try {
            roleService.update(role,rightCodes);
            return "redirect:/role/list";
        } catch (Exception e) {
            e.printStackTrace();
            return "redirect:/role/edit?roleId="+role.getRoleId();
        }
  }
  @ResponseBody
  @RequestMapping("/delete")
 public Object delete(Long roleId){
      Map<String,Object> map=new HashMap<>();
      try {
          roleService.delete(roleId);
          map.put("delResult",true);
      } catch (Exception e) {
          e.printStackTrace();
          map.put("delResult",false);
      }

     return map;
 }

}
